package com.alon.spring01.common.config.spring;

import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @program: spring01
 * @description: 加载spring bean上下文
 * @author: yulongguang
 * @create: 2019-04-27 15:45
 **/
@Slf4j
@Component
public class SpringUtil implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        log.info("注入ApplicationContent到SpringUtil:{}", applicationContext);

        if (SpringUtil.applicationContext != null) {
            log.warn("SpringContextHolder中的ApplicationContext被覆盖，原有ApplicationContext为：{}",
                    SpringUtil.applicationContext);
        }
        SpringUtil.applicationContext = applicationContext;
    }

    /**
     * 取得存储在静态变量中的ApplicationContext
     * @return
     */
    public static ApplicationContext getApplicationContext(){
        assertContextInjected();
        return applicationContext;
    }

    /**
     * 从静态变量applicationContext中取得Bean，自动转型为所赋值对象的类型
     * @param name springbean的id
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(String name) {
        assertContextInjected();
        return (T) applicationContext.getBean(name);
    }

    /**
     * 从静态变量applicationContext中取得bean,自动转型为所赋值对象的类型
     * @param clazz
     * @param <T>
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T getBean(Class<T> clazz) {
        assertContextInjected();
        Map<String, ?> beans = applicationContext.getBeansOfType(clazz);
        T bean = null;
        if (beans != null) {
            bean = (T) beans.get(beans.keySet().iterator().next());
        }
        return bean;
    }

    /**
     * 通过name,以及clazz返回指定的Bean
     * @param name
     * @param clazz
     * @param <T>
     * @return
     */
    public static <T> T getBean(String name, Class<T> clazz) {
        assertContextInjected();
        return applicationContext.getBean(name, clazz);
    }

    /**
     * 从静态变量applicationContext中取得clazz类型bean的map集合
     * @param clazz
     * @return
     */
    public static Map<String, ?> getBeansOfType(Class<?> clazz) {
        assertContextInjected();
        Map<String, ?> beans = applicationContext.getBeansOfType(clazz);
        return beans;
    }

    /**
     * 实现DisposableBean 接口，在Context关闭时清理静态变量
     * @throws Exception
     */
    public void destroy() throws Exception {
        SpringUtil.clear();
    }

    /**
     * 清除SpringUtil中的ApplicationContext为null
     */
    public static void clear() {
        log.debug("清除SpringUtil中的ApplicationContext:{}", applicationContext);
        applicationContext = null;
    }

    /**
     * 检查ApplicationContext不为空.为空抛出异常:IllegalStateException
     */
    public static void assertContextInjected() {
        if (applicationContext == null) {
            throw new IllegalStateException("applicationContext未注入，请在spring.xml或注解中定义SpringUtil");
        }
    }
}
